﻿using J.StatusRepository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatusService
{
    public class TimeService:ITimeService
    {
        private readonly IBookingRepository bookingRepository;
        private readonly IBookingRoomRepository bookingRoomRepository;
        private readonly IOrdersRepository ordersRepository;
        private readonly IRoomTypeRepository roomTypeRepository;
        private readonly IRoomNumManageRepository roomNumManageRepository;
        private readonly IStayRepository stayRepository;
        private readonly IReserveRepository reserveRepository;

        public TimeService(IBookingRepository bookingRepository, IBookingRoomRepository bookingRoomRepository, IOrdersRepository ordersRepository, IRoomTypeRepository roomTypeRepository, IRoomNumManageRepository roomNumManageRepository, IStayRepository stayRepository, IReserveRepository reserveRepository)
        {
            this.bookingRepository = bookingRepository;
            this.bookingRoomRepository = bookingRoomRepository;
            this.ordersRepository = ordersRepository;
            this.roomTypeRepository = roomTypeRepository;
            this.roomNumManageRepository = roomNumManageRepository;
            this.stayRepository = stayRepository;
            this.reserveRepository = reserveRepository;
        }
        /// <summary>
        /// 超时未入住
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateState()
        {
            var list = from a in await bookingRepository.GetAllAsync()
                       join b in await bookingRoomRepository.GetAllAsync() on a.ID equals b.BookingId
                       join c in await ordersRepository.GetAllAsync() on a.ID equals c.BookingId
                       where a.BookingEndTime <= DateTime.Now && c.OrderState == J.StatusModel.OrderState.待入住
                       select c;
            foreach (var item in list)
            {
                var roomNumList = await roomNumManageRepository.GetAllAsync(x=>x.RoomNumber==item.RoomNum);
                var numlist = roomNumList.FirstOrDefault();
                numlist.RoomState = J.StatusModel.RoomState.空房;
                await roomNumManageRepository.UpdateAsync(numlist);

                item.OrderState = J.StatusModel.OrderState.超时未入住;
                await ordersRepository.UpdateAsync(item);
            }
            return 1;
        }
    }
}
